package com.hust.util;

import com.hust.domain.User;
import com.hust.service.LoginAndRegisterService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;


public class BabyRealm extends AuthorizingRealm{

    @Autowired
    LoginAndRegisterService loginAndRegisterService;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }


    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("----------------------test auth---------------------------");

        UsernamePasswordToken usertoken = (UsernamePasswordToken)authenticationToken;

        User user = loginAndRegisterService.searchUserByuName(usertoken.getUsername());

        if(user==null){
            return null;
        }

        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        session.setAttribute("user",user);


        return new SimpleAuthenticationInfo(user,user.getuPwd(),"");
    }
}
